﻿function showDiscuss(pid) {
	if (!sys.canViewDiscuss) return;
	if (refreshTab('showDiscuss' + pid)) return;

	/* region tree */
	var cm = [new Ext.list.Column({
						header : 'Title',
						dataIndex : 'title'
					}), new Ext.list.Column({
						header : 'Length',
						dataIndex : 'len',
						width : 100
					}), new Ext.list.Column({
				header : 'Author',
				dataIndex : 'username',
				width : 100,
				tpl : new Ext.XTemplate('<a href="javascript:void(0);" onclick="showUser(\'{username}\');"'
						+ ' title="{[Ext.isEmpty(values.nickname)?values.username:values.nickname]}" class="likelink">'
						+ '{[Ext.isEmpty(values.nickname)?values.username:values.nickname]}</a>')
			}), new Ext.list.Column({
						header : 'Time',
						dataIndex : 'time',
						width : 200
					})];

	var root = new Ext.tree.AsyncTreeNode({
				id : 'root',
				loader : new Ext.tree.TreeLoader({
							url : url.getDiscussTree,
							baseParams : {
								pid : pid
							}
						}),
				expanded : true
			});

	var tree = new Ext.ux.tree.TreeGrid({
				region : 'center',
				columns : cm,
				root : root
			});
	tree.on('beforeload', function(node, event) {
				if (node.id != 'root') node.loader = new Ext.tree.TreeLoader({
							url : url.getDiscussTree,
							baseParams : {
								did : node.id
							}
						});
			});
	tree.on('click', function(node) {
				showPage(node.id);
			});
	/* endregion */

	/* region newDiscuss */
	var newtitle = new Ext.form.TextField({
				fieldLabel : 'Title',
				allowBlank : false,
				name : 'title',
				anchor : '98%'
			});
	var newcontent = new Ext.form.HtmlEditor({
				fieldLabel : 'Content',
				name : 'content',
				anchor : '98% -30'
			});
	var newform = new Ext.FormPanel({
				frame : true,
				border : false,
				labelAlign : 'right',
				labelWidth : 60,
				items : [newtitle, newcontent],
				fbar : [new Ext.Button({
									text : '重置',
									iconCls : 'reset-icon',
									handler : function() {
										newform.getForm().reset();
									}
								}), new Ext.Button({
									text : '提交',
									iconCls : 'ok-icon',
									disabled : !sys.canDiscuss,
									handler : onpost
								})]
			});
	/* endregion */

	/* region showDiscuss */
	var author = new Ext.form.TextField({
				fieldLabel : 'Author',
				readOnly : true,
				anchor : '100%'
			});
	var time = new Ext.form.TextField({
				fieldLabel : 'Time',
				readOnly : true,
				anchor : '96%'
			});
	var title = new Ext.form.TextField({
				fieldLabel : 'Title',
				readOnly : true,
				anchor : '98%'
			});
	var content = new Ext.ux.HtmlViewer({
				fieldLabel : 'Content',
				anchor : '98% -55'
			});
	var toolbarshow = new Ext.Toolbar({
				items : [new Ext.Button({
									text : '新帖',
									iconCls : 'newdiscuss-icon',
									handler : onnew,
									disabled : !sys.canDiscuss
								}), new Ext.Button({
									text : '回复',
									iconCls : 'replydiscuss-icon',
									handler : onreply,
									disabled : !sys.canDiscuss
								}), new Ext.Toolbar.Separator(), new Ext.Button({
									text : '编辑',
									iconCls : 'editdiscuss-icon',
									disabled : true,
									handler : onedit
								})]
			});
	if (sys.god) {
		toolbarshow.add(new Ext.Button({
					text : '删除',
					iconCls : 'delete-icon',
					handler : ondel
				}));
	}
	var showform = new Ext.FormPanel({
				frame : true,
				border : false,
				labelAlign : 'right',
				labelWidth : 60,
				items : [{
							layout : 'column',
							items : [{
										columnWidth : .49,
										layout : 'form',
										items : author
									}, {
										columnWidth : .51,
										layout : 'form',
										items : time
									}]
						}, title, content]
			});
	var showpanel = new Ext.Panel({
				layout : 'fit',
				bodyBorder : false,
				items : [showform],
				tbar : toolbarshow
			});
	/* endregion */

	var downpanel = new Ext.Panel({
				layout : 'card',
				activeItem : 0,
				split : true,
				height : 170,
				region : 'south',
				bodyBorder : false,
				items : [newform, showpanel]
			});

	var panel = new Ext.Panel({
				id : 'showDiscuss' + pid,
				title : 'Discuss-' + pid,
				layout : 'border',
				closable : true,
				items : [tree, downpanel]
			});
	panel.onRefresh = onRefresh;
	addTab(panel);

	/* region events */
	var did = null;
	var editdata = null;
	var lastPage = newform;
	function showPage(vardid) {
		did = vardid;
		editdata = null;
		showform.getForm().reset();
		getDiscuss({
					did : did,
					success : function(data) {
						author.setValue(sys.rd.authorusername(data));
						title.setValue(data.title);
						time.setValue(data.time);
						content.setValue(data.content);
						toolbarshow.get(3).setDisabled(!(sys.god || sys.user
								&& sys.user.username == data.username));

						if (lastPage != showform) {
							downpanel.getLayout().setActiveItem(1);
							lastPage = showform;
						}
					}
				});
	}
	function newPage() {
		if (lastPage != newform) {
			downpanel.getLayout().setActiveItem(0);
			lastPage = newform;
		}
	}
	function onnew() {
		did = null;
		newform.getForm().reset();
		newPage();
	}
	function onreply() {
		newtitle.setValue('Re:' + title.getValue());
		newcontent.reset();
		newPage();
	}
	function onedit() {
		getDiscuss({
					did : did,
					success : function(data) {
						editdata = data;
						newtitle.setValue(data.title);
						newcontent.setValue(data.content);
						newPage();
					}
				});
	}
	function ondel() {
		deleteDiscuss({
					did : did,
					title : title.getValue(),
					success : function() {
						onRefresh(true);
						onnew();
					}
				});
	}
	function onRefresh(isdel) {
		var node = tree.getSelectionModel().getSelectedNode();
		if (node == null) node = tree.getRootNode();
		if (isdel) node = node.parentNode;
		var path = node.getPath();
		if (node.isLeaf()) node = node.parentNode;
		reloadnode();

		function reloadnode() {
			if (node.id != 'root') {
				var thisnode = node;
				node = node.parentNode;
				thisnode.reload(reloadnode);
			} else
				node.reload(function() {
							tree.selectPath(path);
						});
		}
	}
	function onpost() {
		if (!newform.getForm().isValid() || !isNoBlank(newcontent)) {
			alertx('请正确输入内容！');
			return;
		}
		var posttype = editdata ? 2 : did ? 1 : 0;
		var params = {
			pid : pid
		};
		if (posttype == 2) {
			params.did = did;
			params.parentDid = editdata.parentDid;
		} else if (posttype == 1) params.parentDid = did;

		ajaxform({
					form : newform,
					url : url.addOrEditDiscuss,
					params : params,
					success : function(obj) {
						showPage(obj.did);
						onRefresh();
					}
				});
	}
	/* endregion */
}

function deleteDiscuss(obj) {
	confirmx('您确定要删除帖子 [' + obj.did + ']' + (obj.title ? '<br />' + obj.title + '<br />' : '')
					+ '吗？该帖子的子贴也会一并删除。', function() {
				ajax({
							url : url.deleteDiscuss,
							params : {
								did : obj.did
							},
							success : obj.success
						});
			});
}

function getDiscuss(obj) {
	ajax({
				url : url.getDiscuss,
				params : {
					did : obj.did
				},
				success : obj.success
			});
}
